package Meins;

import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.SpotLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Taschenlampe extends JFrame {

	private BranchGroup rootBranchGroup;
	private TransformGroup rootTransformGroup;
	private Canvas3D canvas;
	private SimpleUniverse simpUniv;
	private final float sl = 0.1f;

	public Taschenlampe() {
		super();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		rootBranchGroup = new BranchGroup();
		rootTransformGroup = new TransformGroup();
		rootBranchGroup.addChild(rootTransformGroup);
		rootTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
		simpUniv = new SimpleUniverse(canvas);
		simpUniv.getViewingPlatform().setNominalViewingTransform();
		createSceneGraph(simpUniv);

		mausverhalten();

		setSize(1000, 700);
		getContentPane().add("Center", canvas);
		setVisible(true);

	}

	public void mausverhalten() {

		OrbitBehavior ob = new OrbitBehavior(canvas);
		ob.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
				Double.MAX_VALUE));
		simpUniv.getViewingPlatform().setViewPlatformBehavior(ob);
	}

	public void createSceneGraph(SimpleUniverse su) {

		// Farben

		Appearance yellowApp = new Appearance();
		setToMyDefaultAppearance(yellowApp, new Color3f(1.0f, 1.0f, 0.0f));
		Appearance weissApp = new Appearance();
		setToMyDefaultAppearance(weissApp, new Color3f(0.3f, 0.3f, 0.3f));
		Appearance kegelApp = new Appearance();
		setToMyDefaultAppearance(kegelApp, new Color3f(0.5f, 0.5f, 0.5f));
		Appearance brownApp = new Appearance();
		setToMyDefaultAppearance(brownApp, new Color3f(0.5215f, 0.270588f,
				0.07450f));
		Appearance blackApp = new Appearance();
		setToMyDefaultAppearance(blackApp, new Color3f(0f, 0f, 0f));

		// Transformgrp

		Transform3D tfszene = new Transform3D();
		tfszene.rotZ(Math.PI / 2);
		TransformGroup tgszene = new TransformGroup(tfszene);
		tgszene.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

		// Objekte

		Cylinder Taschenlampe = new Cylinder(0.1f, 0.5f, brownApp);
		Transform3D tfc = new Transform3D();
		tfc.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
		TransformGroup tgc = new TransformGroup(tfc);
		tgc.addChild(Taschenlampe);

		Cylinder TaschenlampenKopf = new Cylinder(0.11f, 0.3f, blackApp);
		Transform3D tfck = new Transform3D();
		tfck.setTranslation(new Vector3f(0.0f, -0.4f, 0.0f));
		TransformGroup tgck = new TransformGroup(tfck);
		tgck.addChild(TaschenlampenKopf);

		Cylinder Taschenlampendeckel = new Cylinder(0.1f, 0.1f, weissApp);
		Transform3D tfcd = new Transform3D();
		tfcd.setTranslation(new Vector3f(0.0f, -0.5001f, 0.0f));
		TransformGroup tgcd = new TransformGroup(tfcd);
		tgcd.addChild(Taschenlampendeckel);

		Sphere Knopf = new Sphere(0.02f, Sphere.GENERATE_NORMALS, 100, blackApp);
		Transform3D tfk = new Transform3D();
		tfk.setTranslation(new Vector3f(0.09f, -0.18f, 0.0f));
		TransformGroup tgk = new TransformGroup(tfk);
		tgk.addChild(Knopf);
		Cone Kegel = new Cone (0.2f,0.5f,Cone.GENERATE_NORMALS,200,200,kegelApp);
		//Sphere Kegel = new Sphere(0.5f, Sphere.GENERATE_NORMALS, 100, kegelApp);
		Transform3D tft = new Transform3D();
		tft.rotZ(Math.PI+Math.PI/2);
		tft.setTranslation(new Vector3f(0.0f, -1.5f, 0.0f));
		TransformGroup tgt = new TransformGroup(tft);
		tgt.addChild(Kegel);
		
		// Licht
		BranchGroup bgLight = new BranchGroup();
		BoundingSphere boundsl = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
				100.0);
		Color3f lightColour3 = new Color3f(1f, 0f, 0f);
		
		SpotLight light2 = new SpotLight(lightColour3, new Point3f(0.7f,
				0.0f, 0.0f), new Point3f(0.1f, 0.1f, 0.01f), new Vector3f(
				-1.0f, 0.0f, 0.0f), (float) (Math.PI / 15), 0.0f);
		light2.setInfluencingBounds(boundsl);
		
		SpotLight light3 = new SpotLight(lightColour3, new Point3f(0.51f,
				0.0f, 0.0f), new Point3f(0.1f, 0.1f, 0.01f), new Vector3f(
				1.0f, 0.0f, 0.0f), (float) (Math.PI / 15), 0.0f);
		light3.setInfluencingBounds(boundsl);
		
		Vector3f lightDir1 = new Vector3f(-1.0f, 0.0f, -0.5f);
		DirectionalLight light1 = new DirectionalLight(lightColour3, lightDir1);
		light1.setInfluencingBounds(boundsl);
		
		
		
		//bgLight.addChild(light1);
		bgLight.addChild(light2);
		bgLight.addChild(light3);
		su.addBranchGraph(bgLight);

		
		



		// Adden
		tgszene.addChild(tgt);
		//tgszene.addChild(tgt2);
		tgszene.addChild(tgc);
		tgszene.addChild(tgck);
		tgszene.addChild(tgcd);
		tgszene.addChild(tgk);
		rootTransformGroup.addChild(tgszene);

		// Hintergrund
		Background bg = new Background(new Color3f(0.2f, 0.2f, 0.2f));
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
				Double.MAX_VALUE);
		bg.setApplicationBounds(bounds);
		rootBranchGroup.addChild(bg);
		rootBranchGroup.compile();
		su.addBranchGraph(rootBranchGroup);
	}


	public static void setToMyDefaultAppearance(Appearance app, Color3f col) {
		app.setMaterial(new Material(col, col, col, col, 150.0f));
	}

	public static void main(String[] args) {

		new Taschenlampe();

	}

}
